package com.wanmait.schedule;

import com.wanmait.schedule.interceptor.LeaderJWTInterceptor;
import com.wanmait.schedule.interceptor.LoginInterceptor;
import com.wanmait.schedule.interceptor.MinisterJWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private MinisterJWTInterceptor ministerJWTInterceptor;
    @Resource
    private LeaderJWTInterceptor leaderJWTInterceptor;

    //设置静态资源的处理，请求路径和存放路径的对应关系
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // /static/uploadImages/1.jpg   => src/main/resources/static/uploadImages/1.jpg
        // /static/uploadImages/abc/12.jpg   => src/main/resources/static/uploadImages/abc/12.jpg
        registry.addResourceHandler("/static/uploadFiles/**").addResourceLocations("classpath:/static/uploadFiles/");
        // /uploadImages/1.jpg => D:/uploadImages/1.jpg
        registry.addResourceHandler("/static/uploadFiles/headerPics/**").addResourceLocations("classpath:/static/uploadFiles/headerPics/");
    }
    //配置拦截器
    /*  springMVC.xml中配置拦截器
       <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/manage/**"/>
            <mvc:mapping path="/admin/**"/>
            <mvc:exclude-mapping path="/manage/login"/>
            <!--<ref bean="loginInterceptor"></ref>-->
            <bean class="com.wanmait.mavendemo.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>*/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(loginInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/login");
//        registry.addInterceptor(jwtInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/admin/login");
        registry.addInterceptor(ministerJWTInterceptor).addPathPatterns("/*/minister/**","/userinfo/showIndex").excludePathPatterns("/UserLoginView");
        registry.addInterceptor(leaderJWTInterceptor).addPathPatterns("/*/leader/**","/userinfo/showIndex").excludePathPatterns("*/UserLoginView");
    }

    //配置跨域请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //.allowedOrigins("http://www.wanmait.com","http://www.qingruanit.com")
                .allowedOrigins("*")
                //.allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600);//3600秒 在一个小时内不需要重复发送预检请求 缓存
    }
}
